package com.z.service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.db4o.ObjectContainer;
import com.z.base.DbBase;
import com.z.base.LoginUser;
import com.z.dao.FileKeyWordDao;
import com.z.dao.UploadFileDao;
import com.z.dictionary.ResultState;
import com.z.entity.FileKeyWord;
import com.z.entity.UploadFile;
import com.z.util.AttachUtil;
import com.z.util.FileUtil;
import com.z.util.MD5;
import com.z.util.convert.Convert;
import com.z.util.convert.ConvertManager;
import com.z.vo.ViewForm;

public class ViewService {
	private static ViewService viewService;
	
	public static ViewService getInstance(){
		if(viewService==null){
			viewService=new ViewService();
		}
		return viewService;
	}
	
	public void findDocument(ViewForm viewForm){
		UploadFile uploadFile=UploadFileDao.getInstace().get(viewForm.getDocId());
		if(uploadFile!=null&&uploadFile.getUploadTime()!=null){
			viewForm.setSummary(uploadFile.getSummary());
			viewForm.setDocName(uploadFile.getName());
			viewForm.setSwfPath(uploadFile.getSwfPath());
			viewForm.setKeyWords(FileKeyWordDao.getInstace().findKeyWords(uploadFile.getId()));
		}
	}
	
	public void findList(ViewForm viewForm){
		List<String> list=FileKeyWordDao.getInstace().matchUploadFileId(viewForm.getQueryParam());
		UploadFileDao.getInstace().find(viewForm.getQueryParam(), list, viewForm.getPage());
	}
	
	public void delete(String id){
		UploadFileDao.getInstace().delete(id);
	}
	
	public void saveUploadFile(ViewForm viewForm,LoginUser user){
		if(viewForm.getUpload()==null){
			viewForm.setState(ResultState.Fail.ordinal());
			viewForm.setMessage("无文件...");
			return ;
		}
		
		ObjectContainer db = null;
		try {
			String md5=MD5.getFileMD5String(viewForm.getUpload());
			if(UploadFileDao.getInstace().fileExistedByMD5(md5)){
				viewForm.setState(ResultState.Existed.ordinal());
				return ;
			}
			db=DbBase.getInstace().getDB();

			String path=AttachUtil.getInstance().save(viewForm.getUpload(),FileUtil.getExtension(viewForm.getUploadFileName()));
			UploadFile uploadFile=new UploadFile();
			uploadFile.setId(md5);
			uploadFile.setName(viewForm.getUploadFileName());
			uploadFile.setPath(path);
			uploadFile.setSummary(viewForm.getSummary());
			uploadFile.setUploadTime(new Date(System.currentTimeMillis()));
			if(user!=null){
				uploadFile.setUploadBy(user.getUsrId());
			}
			db.store(uploadFile);
			
			for(int i=0;i<viewForm.getKeyWords().length;i++){
				String keyWord=viewForm.getKeyWords()[i];
				if(keyWord==null||keyWord.trim().length()==0){
					continue;
				}
				FileKeyWord fileKeyWord=new FileKeyWord();
				fileKeyWord.setId(UUID.randomUUID().toString());
				fileKeyWord.setKeyWord(keyWord);
				fileKeyWord.setUploadFileId(md5);
				db.store(fileKeyWord);
			}
			viewForm.setState(ResultState.OK.ordinal());
			db.commit();
			// 启动转换线程 ---NEXT！
			ConvertManager convertManager=ConvertManager.getInstance();
			convertManager.execute(new Convert(uploadFile,convertManager.getOfficeManager()));
		} catch (Exception e) {
			viewForm.setState(ResultState.Fail.ordinal());
			viewForm.setMessage(e.toString());
		}
	}
}
